package com.hbt.myplus.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hbt.myplus.model.PetOwn;
import com.hbt.myplus.service.impl.PetOwnServiceImpl;
import com.hbt.myplus.utils.ExcelUtil;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import com.hbt.myplus.service.IPayService;
import com.hbt.myplus.service.impl.PayServiceImpl;
import com.hbt.myplus.model.Pay;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.hbt.myplus.utils.ResponseWeb;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

/**
 * <p>
    *  前端控制器
    * </p>
 *
 * @author astupidcoder
 * @since 2020-12-22
 * @version v1.0
 */
@RestController
@RequestMapping("pay")


public class PayController {

    private Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    private PayServiceImpl payService;
    @Autowired
    private PetOwnServiceImpl petOwnService;

    /**
     * 查询分页数据
     */
    @GetMapping(value = "/s")
    public ResponseWeb<Page> findListByPage(@RequestParam(name = "pageNum", defaultValue = "1") int pageNum,@RequestParam(name = "pageSize", defaultValue = "20") int pageSize){
        return null;
    }
    /**
     * 导出excel
     */
    @PostMapping(value = "/excel")
    public void exportExcel(HttpServletResponse response){
        List<Pay> payList = payService.list(new QueryWrapper<Pay>().orderByDesc("time"));
        List<PetOwn> petOwnList = petOwnService.list();
        List<List<String>> dataList = new ArrayList<>();
        if (payList != null && payList.size()>0){
            payList.forEach(a -> {
                List<PetOwn> petOwns = null;
                if (petOwnList != null && petOwnList.size() > 0){
                    petOwns = petOwnList.stream().filter(b -> b.getOpenid().equals(a.getPoid())).collect(Collectors.toList());
                }
                if (petOwns != null && !petOwns.isEmpty()){
                    a.setPetOwn(petOwns.get(0));
                }
            });
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            for (Pay p : payList) {
                List<String> listItem = new ArrayList<>();
                listItem.add(p.getSno());
                listItem.add(format.format(p.getTime()));
                listItem.add("暂无");
                listItem.add(p.getPrice().toString());
                listItem.add(p.getItemname());
                listItem.add(p.getPetOwn().getNickname());
                listItem.add(p.getPetOwn().getRealname());
                dataList.add(listItem);
            }
        }
        List<String> titleList = Arrays.asList("序号","缴费单号", "时间", "备注", "金额", "类型", "用户昵称",  "用户真名");
        ExcelUtil.uploadExcelAboutUser(response,"项目信息.xlsx",titleList,dataList);
    }

    /**
     * 查询分页数据
     */
    @GetMapping(value = "/all")
    public ResponseWeb all(@RequestHeader(value = "openid", required = false)String openid){
        List<Pay> payList;
        if (openid == null || openid.equals("")){
            payList = payService.list(new QueryWrapper<Pay>().orderByDesc("time"));
        }else {
            payList = payService.list(new QueryWrapper<Pay>().eq("poid", openid).orderByDesc("time"));
        }
        List<PetOwn> petOwnList = petOwnService.list();
        payList.forEach(a -> {
            List<PetOwn> petOwns = petOwnList.stream().filter(b -> b.getOpenid().equals(a.getPoid())).collect(Collectors.toList());
            if (petOwns != null && !petOwns.isEmpty()){
                a.setPetOwn(petOwns.get(0));
            }
        });

        return new ResponseWeb(payList);
    }

    /**
     * 根据id查询
     */
    @GetMapping(value = "/{id}")
    public ResponseWeb<Pay> getById(@PathVariable("id") String id){
       return null;
    }

    /**
     * 新增
     */
    @PostMapping
    public ResponseWeb<Pay> add(@RequestBody Pay pay){
        return null;
    }

    /**
     * 删除
     */
    @DeleteMapping
    public ResponseWeb<String> delete(@RequestParam("ids") List<String> ids){
        return null;
    }

    /**
     * 修改
     */
    @PutMapping
    public ResponseWeb<Pay> update(@RequestBody Pay pay){
        return null;
     }

}
